﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.WPF.Theme.CustomControl.MdiContainer
 * 唯一标识：de97b29a-ffa7-4da8-b75f-f1989c5eb0c9
 * 文件名：MdiContainer
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/8/21 11:16:45
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// MdiContainer.xaml 的交互逻辑
/// </summary>
public partial class MdiContainer : UserControl
{
    public MdiContainer()
    {
        InitializeComponent();
    }


    /// <summary>
    /// 子窗体集合
    /// </summary>
    public ObservableCollection<IMdiChildViewBase> MdiChildViews
    {
        get { return (ObservableCollection<IMdiChildViewBase>)GetValue(MdiChildViewsProperty); }
        set { SetValue(MdiChildViewsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MdiChildViews.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MdiChildViewsProperty =
        DependencyProperty.Register("MdiChildViews", typeof(ObservableCollection<IMdiChildViewBase>), typeof(MdiContainer), new PropertyMetadata(null,onLoadMdiChildCallback));

    private static void onLoadMdiChildCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var Container = d as MdiContainer;

        if (e.NewValue != null&&e.NewValue is ObservableCollection<IMdiChildViewBase> views)
        {
            //views.CollectionChanged += Views_CollectionChanged;

            foreach (var item in views)
            {
                Container.CreateIconButton(item);
            }
        }

        //Container.childBtns
    }

    private static void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                {
                    foreach (var item in e.NewItems)
                    {
                        if(item is IMdiChildViewBase view)
                        {
                            
                        }
                    }
                }
                break;
            case NotifyCollectionChangedAction.Remove:
                break;
            case NotifyCollectionChangedAction.Replace:
                break;
            case NotifyCollectionChangedAction.Move:
                break;
            case NotifyCollectionChangedAction.Reset:
                break;
            default:
                break;
        }
    }



    #region 创建mdi中的按钮

    private Dictionary<string, IMdiChildViewBase> ViewDic { get; set; } = new Dictionary<string, IMdiChildViewBase>();

    private void CreateIconButton(IMdiChildViewBase view)
    {
        //var type= view.GetType();
        //var tokenAttr= type.GetCustomAttribute<VampirewalIoCRegisterAttribute>();

        //ViewDic.Add(tokenAttr.Token, view);


        Grid grid = new Grid()
        {
            Cursor = Cursors.Hand,
            ToolTip=view.Title,
            Tag= view.ViewId.ToString()
        };
        grid.RowDefinitions.Add(new RowDefinition());
        grid.RowDefinitions.Add(new RowDefinition() { Height=new GridLength(30)});

        grid.MouseLeftButtonDown += Grid_MouseLeftButtonDown;



        Border icon = new Border()
        {
            Height=20,
            Width=20,
        };

        BitmapImage bitmapImage = new BitmapImage();
        // 使用UriSource属性设置图像路径
        bitmapImage.BeginInit();
        bitmapImage.UriSource = new Uri(view.IconPath, UriKind.RelativeOrAbsolute);
        bitmapImage.EndInit();

        // 创建一个ImageBrush对象
        ImageBrush imageBrush = new ImageBrush();
        imageBrush.ImageSource = bitmapImage;

        icon.Background = imageBrush;
        Grid.SetRow(icon, 0);

        grid.Children.Add(icon);


        TextBlock title= new TextBlock();
        title.Text = view.Title;
        title.HorizontalAlignment = HorizontalAlignment.Center;
        title.VerticalAlignment = VerticalAlignment.Center;

        grid.Children.Add(title);
        Grid.SetRow(title, 1);

        childBtns.Children.Add(grid);
    }

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var grid= sender as Grid;

        var viewGuid= Guid.Parse(grid.Tag.ToString());

        var childView=(UserControl) MdiChildViews.FirstOrDefault(f=>f.ViewId==viewGuid);

        if (!Mdi.Children.Contains(childView))
        {
            Mdi.Children.Add(childView);
        }
        else
        {
            
        }
        
    }

    #endregion
}
